layui.use(['table', 'admin', 'ax', 'func', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var layer = layui.layer;

    /**
     * 接口审批列表管理
     */
    var Interfaceapproval = {
        tableId: "interfaceapprovalTable"
    };

    /**
     * 初始化表格的列
     */
    Interfaceapproval.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'id'},
            {
                field: 'rank',
                title: '序号',
                align: 'center',
                templet: '#rank',
                style: 'background-color:#ffffff',
                width: 120
            },
            {field: 'userId', sort: true, title: '用户id', hide: true},
            {
                field: 'content', sort: false, title: '申请内容',
                style: 'background-color:#ffffff',
                align: 'center',
                templet: function (res) {
                    if (res.content == 1) {
                        return '数据对比接口'
                    } else if (res.content == 2) {
                        return '接口定制'
                    }
                }
            },
            {
                field: 'department', sort: false, title: '单位',
                style: 'background-color:#ffffff',
                align: 'center'
            },
            {
                field: 'system', sort: false, title: '应用系统',
                style: 'background-color:#ffffff',
                align: 'center'
            },
            {
                field: 'servicetype', sort: false, title: '服务类型',
                style: 'background-color:#ffffff',
                align: 'center',
                templet: function (res) {
                    if (res.servicetype == 1) {
                        return '数据库服务'
                    }
                    // else if (res.servicetype == 2) {
                    //     return '平台类型'
                    // } else if (res.servicetype == 3) {
                    //     return '其他类型'
                    // }
                }
            },
            {
                field: 'idescribe', sort: false, title: '接口描述',
                style: 'background-color:#ffffff',
                align: 'center'
            },
            {
                field: 'state', sort: false, title: '状态',
                style: 'background-color:#ffffff',
                align: 'center',
                templet: function (res) {
                    if (res.state == 0) {
                        return '<span style="color:#FF4500;">' + '待处理' + '</span>'
                    } else if (res.state == 1) {
                        return '<span style="color:greenyellow">' + '已批准,等待结果' + '</span>'
                    } else if (res.state == 2) {
                        return '<span style="color:#c00;">' + '已驳回' + '</span>'
                    } else if (res.state == 3) {
                        return '<span style="color:#008000;">' + '已完成' + '</span>'
                    }
                }
            },
            {field: 'remarks', sort: true, title: '备注', hide: true},
            {field: 'revision', sort: true, title: '乐观锁', hide: true},
            {field: 'createdBy', sort: true, title: '创建人', hide: true},
            {field: 'createdTime', sort: true, title: '创建时间', hide: true},
            {field: 'updatedBy', sort: true, title: '更新人', hide: true},
            {field: 'updatedTime', sort: true, title: '更新时间', hide: true},
            {
                align: 'right', toolbar: '#tableBar', title: '',
                style: 'background-color:#ffffff'
            }
        ]];
    };

    /**
     * 点击查询按钮
     */
    Interfaceapproval.search = function () {
        var queryData = {};

        queryData['content'] = $('#checkContent input[name="content"]:checked ').val();
        queryData['system'] = $('#system').val();
        queryData['servicetype'] = $('#servicetype').val();
        queryData['state'] = $('#state').val();

        table.reload(Interfaceapproval.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击新增接口定制
     */
    $('#addInterface').click(function () {
        window.location.href = Feng.ctxPath + '/interfaceapproval/addInterfaceCustom'
    })

    /**
     * 点击新增数据对比
     */
    $('#addData').click(function () {
        window.location.href = Feng.ctxPath + '/interfaceapproval/addInterfaceDataCompare'
    })


    /**
     * 点击查看详情
     *
     * @param data 点击按钮时候的行数据
     */
    Interfaceapproval.openEditDlg = function (data) {
        if (data.content == 2) {
            sessionStorage.setItem("interfaceapprovalId", data.id)
            layer.open({
                title: '',
                area: ['80%', '80%'],
                content: Feng.ctxPath + '/interfaceapproval/interfaceCustomDone',
                type: 2,
            });
        } else {
            sessionStorage.setItem("messageId", data.id)
            layer.open({
                title: '',
                area: ['80%', '80%'],
                content: Feng.ctxPath + '/interfaceapproval/queryInterfaceData',
                type: 2,
            });
        }

    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Interfaceapproval.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/interfaceapproval/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Interfaceapproval.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var userId = '';
    $.ajax(Feng.ctxPath + "/databaseListKh/getUser", {
        async: false,
        success: function (res) {
            // console.log(res.data)
            userId = res.data.id
        }
    })
    // console.log(userId)

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Interfaceapproval.tableId,
        url: Feng.ctxPath + '/interfaceapproval/list?userId=' + userId.toString(),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: 'line',
        even: false,
        cols: Interfaceapproval.initColumn(),
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Interfaceapproval.search();
    });

    // 工具条点击事件
    table.on('tool(' + Interfaceapproval.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Interfaceapproval.openEditDlg(data);
        }
        // else if (layEvent === 'delete') {
        //     Interfaceapproval.onDeleteItem(data);
        // }
    });
});
